package com.yx.frame.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.LineNumberReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.Vector;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import java.util.jar.*;


public class FileTool {
	String path = null;

	public FileTool(String path) {
		this.path = path;
	}

	public boolean write(String buf, boolean append) throws Exception {
		FileWriter file;
		try {
			file = new FileWriter(path, append);
			file.write(buf);
		} catch (Exception e) {
			String err = "д�ļ�[" + path + "]" + "����:" + e;
			throw new Exception(err);
		}
		file.close();
		return true;
	}

	public String readContent() throws Exception {
		LineNumberReader lread = null;
		try {
			lread = new LineNumberReader(new FileReader(path));
		} catch (FileNotFoundException e) {
			throw new Exception("�����ļ�ʧ��" + e);
		}
		StringBuffer sb = new StringBuffer();
		while (true) {
			String line = null;
			try {
				line = lread.readLine();
			} catch (Exception e) {
			}
			if (line == null) {
				lread.close();
				return sb.toString();
			}
			sb.append(line).append("\r\n");
		}
	}

	public boolean isDirectory() {
		File f = new File(path);
		return f.isDirectory();
	}

	public boolean delete() {
		File f = new File(path);
		return f.delete();
	}

	public String[] list() {
		File f = new File(path);
		return f.list();
	}

	private int BUFFER_LEN = 2000000;

	
	public void compressFile(String[] fileName, String zipName)
			throws Exception {
		FileOutputStream fOut = new FileOutputStream(zipName, false);
		ZipOutputStream zOut = new ZipOutputStream(fOut);
		for (int i = 0; i < fileName.length; i++) {
			File f = new File(fileName[i]);
			if (!(f.exists())) {
				zOut.close();
				fOut.close();
				throw new Exception("Դ�ļ������ڣ��ļ���" + fileName[i]);
			}
			FileInputStream in = new FileInputStream(f);
			ZipEntry e = new ZipEntry(fileName[i]); // fileName[i]);
			zOut.putNextEntry(e);
			long remain = f.length();
			// int len=2000000;
			while (remain > 0) {
				int len = BUFFER_LEN;
				if (remain < BUFFER_LEN) {
					len = Integer.parseInt(String.valueOf(remain));
				}
				byte[] buffer = new byte[len];
				in.read(buffer);
				zOut.write(buffer);
				remain = remain - len;
			}
			in.close();
			zOut.closeEntry();
		}
		zOut.close();
		fOut.close();

	}

	/**
	 * ��ѹ���ļ�
	 */
	public Vector deCompressFile(String zipFileName) throws Exception {
		Vector fileNameList = new Vector();

		File file = new File(zipFileName);
		if (!(file.exists())) {
			throw new Exception("ѹ���ļ������ڣ��ļ���" + zipFileName);
		}
		ZipInputStream in = new ZipInputStream(new FileInputStream(file));
		ZipEntry e = null;

		while ((e = in.getNextEntry()) != null) {
			String entryName = e.getName();
			if (entryName.length() < 1) {
				in.closeEntry();
				in.close();
				throw new Exception("ѹ���ļ����ڴ����ļ���" + zipFileName + ":"
						+ entryName);
			}
			fileNameList.addElement(entryName);
			FileOutputStream out = new FileOutputStream(entryName, false);

			byte[] buffer = new byte[BUFFER_LEN];
			int len = 0;
			while ((len = in.read(buffer)) > 0) {
				byte[] b = new byte[len];
				System.arraycopy(buffer, 0, b, 0, len);
				out.write(b);
			}
			out.close();
			in.closeEntry();
		}
		in.close();
		return fileNameList;

	}

	public static Serializable readObjectFile(String fileName) throws Exception {
		Serializable obj;
		try {
			FileInputStream in = new FileInputStream(fileName);
			ObjectInputStream input = new ObjectInputStream(in);
			obj = (Serializable) input.readObject();
			in.close();
			input.close();
			return obj;
		} catch (Exception e) {
			throw e;
		}
	}

	public static void writeObjectFile(Serializable obj, String fileName)
			throws Exception {
		try {
			FileOutputStream out = new FileOutputStream(fileName);
			ObjectOutputStream output = new ObjectOutputStream(out);
			output.writeObject(obj);
			output.close();
			out.close();
		} catch (Exception e) {
			throw e;
		}
	}

	public static void jarDir(String dirPath, String zipFileName)
			throws Exception {
		OutputStream os = null;
		JarOutputStream jarOut=null;
		try {
			Manifest manifest=new Manifest();
			os = new FileOutputStream(zipFileName);
		    jarOut = new JarOutputStream( os ); 
			jar(dirPath, new File(dirPath), jarOut, true, true);
		} catch (Exception e) {
			throw e;
		} finally {
			if (jarOut != null) {
				jarOut.close();
			}
			if (os != null) {
				os.close();
			}
		}
		JarFile jarFile=new JarFile(zipFileName);
	}
	public static void jar(String path, File basePath, JarOutputStream zo,
			boolean isRecursive, boolean isOutBlankDir) throws Exception {

		File inFile = new File(path);

		File[] files = new File[0];
		if (inFile.isDirectory()) { // ��Ŀ¼
			files = inFile.listFiles();
		} else if (inFile.isFile()) { // ���ļ�
			files = new File[1];
			files[0] = inFile;
		}
		byte[] buf = new byte[1024];
		int len;
		// System.out.println("baseFile: "+baseFile.getPath());
		for (int i = 0; i < files.length; i++) {
			String pathName = "";
			if (basePath != null) {
				if (basePath.isDirectory()) {
					pathName = files[i].getPath().substring(
							basePath.getPath().length() + 1);
				} else {// �ļ�
					pathName = files[i].getPath().substring(
							basePath.getParent().length() + 1);
				}
			} else {
				pathName = files[i].getName();
			}
			System.out.println(pathName);
			if (files[i].isDirectory()) {
				if (isOutBlankDir && basePath != null) {
					zo.putNextEntry(new JarEntry(pathName + "/")); // ����ʹ��Ŀ¼Ҳ�Ž�ȥ
				}
				if (isRecursive) { // �ݹ�
					zip(files[i].getPath(), basePath, zo, isRecursive,
							isOutBlankDir);
				}
			} else {
				FileInputStream fin = new FileInputStream(files[i]);
				zo.putNextEntry(new JarEntry(pathName));
				while ((len = fin.read(buf)) > 0) {
					zo.write(buf, 0, len);
				}
				fin.close();
			}
		}
	}

	public static void zipDir(String dirPath, String zipFileName)
			throws Exception {
		OutputStream os = null;
		BufferedOutputStream bs = null;
		ZipOutputStream zo = null;
		try {
			os = new FileOutputStream(zipFileName);
			bs = new BufferedOutputStream(os);
			zo = new ZipOutputStream(bs);
			zip(dirPath, new File(dirPath), zo, true, true);
		} catch (Exception e) {
			throw e;
		} finally {
			if (zo != null) {
				zo.close();
			}
			if (bs != null) {
				bs.close();
			}
			if (os != null) {
				os.close();
			}
		}
	}

	public static void zip(String path, File basePath, ZipOutputStream zo,
			boolean isRecursive, boolean isOutBlankDir) throws Exception {

		File inFile = new File(path);

		File[] files = new File[0];
		if (inFile.isDirectory()) { // ��Ŀ¼
			files = inFile.listFiles();
		} else if (inFile.isFile()) { // ���ļ�
			files = new File[1];
			files[0] = inFile;
		}
		byte[] buf = new byte[1024];
		int len;
		// System.out.println("baseFile: "+baseFile.getPath());
		for (int i = 0; i < files.length; i++) {
			String pathName = "";
			if (basePath != null) {
				if (basePath.isDirectory()) {
					pathName = files[i].getPath().substring(
							basePath.getPath().length() + 1);
				} else {// �ļ�
					pathName = files[i].getPath().substring(
							basePath.getParent().length() + 1);
				}
			} else {
				pathName = files[i].getName();
			}
			// System.out.println(pathName);
			if (files[i].isDirectory()) {
				if (isOutBlankDir && basePath != null) {
					zo.putNextEntry(new ZipEntry(pathName + "/")); // ����ʹ��Ŀ¼Ҳ�Ž�ȥ
				}
				if (isRecursive) { // �ݹ�
					zip(files[i].getPath(), basePath, zo, isRecursive,
							isOutBlankDir);
				}
			} else {
				FileInputStream fin = new FileInputStream(files[i]);
				zo.putNextEntry(new ZipEntry(pathName));
				while ((len = fin.read(buf)) > 0) {
					zo.write(buf, 0, len);
				}
				fin.close();
			}
		}
	}

	public static void delDir(String filepath) throws Exception {
		File f = new File(filepath);// �����ļ�·��
		if (f.exists() && f.isDirectory()) {// �ж����ļ�����Ŀ¼
			if (f.listFiles().length == 0) {// ��Ŀ¼��û���ļ���ֱ��ɾ��
				f.delete();
			} else {// ��������ļ��Ž����飬���ж��Ƿ����¼�Ŀ¼
				File delFile[] = f.listFiles();
				int i = f.listFiles().length;
				for (int j = 0; j < i; j++) {
					if (delFile[j].isDirectory()) {
						delDir(delFile[j].getAbsolutePath());// �ݹ����del������ȡ����Ŀ¼·��
					}
					delFile[j].delete();// ɾ���ļ�
				}
			}
			delDir(filepath);// �ݹ����
		}

	}
}
